package powerControl.web.interceptor;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

import powerControl.entity.User;
import powerControl.service.IUserService;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class PowerInterceptor extends AbstractInterceptor{
	
	private IUserService userService;
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//定义结果字符串，默认跳转到主页
		String result = "main";
		
		//得到资源路径
		String url = ServletActionContext.getRequest().getRequestURI();
		url = url.substring(url.lastIndexOf("/") + 1, url.indexOf(".action") );
		
		System.out.println("\n\n\n-----------url:  " + url + "\n\n\n");
		
		//得到已登录的用户
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		//通过权限审查，可以继续访问
		if (userService.checkPowerForUserAndUrl(user, url) ) {
			result = invocation.invoke();
		}
		return result;
	}
	
	
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

}